<script setup lang="ts">
import { ref } from 'vue'
import { useDict } from './hook'
import ImportTableDialog from './components/import-table-dialog.vue'

import { useRenderIcon } from '@/components/ReIcon/src/hooks'
import { useTableHeight } from '@/hooks/useTableHeight'
import Search from '@iconify-icons/ep/search'
import Refresh from '@iconify-icons/ep/refresh'
import More from '@iconify-icons/ep/more-filled'

defineOptions({
  name: 'Post',
})

const formRef = ref()
const {
  preview,
  loading,
  columns,
  dataList,
  pagination,
  queryParams,
  showPreviewDialog,
  showImportTableDialog,
  toggleShowImportTableDialog,
  copyTextSuccess,
  handleGenTable,
  handleEditTable,
  handlePreview,
  handleSynchDb,
  onSearch,
  resetForm,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
} = useDict()
const { tableHeight, tableRef } = useTableHeight()
</script>

<template>
  <div class="main table-view-layout">
    <el-form
      ref="formRef"
      :inline="true"
      :model="queryParams"
      class="bg-el-bg w-[99/100] pl-8 pt-4"
    >
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          class="!w-[180px]"
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input
          v-model="queryParams.tableComment"
          placeholder="请输入表描述"
          clearable
          class="!w-[180px]"
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)"> 重置 </el-button>
      </el-form-item>
    </el-form>
    <PureTableBar title="代码生成">
      <template #buttons>
        <el-button type="primary" @click="handleGenTable"> 生成 </el-button>
        <el-button type="primary" @click="toggleShowImportTableDialog(true)">导入</el-button>
        <!-- <el-button type="primary" @click="handleEditTable"> 修改 </el-button> -->
        <!-- <el-button type="primary" @click="handleDelete"> 删除 </el-button> -->
      </template>
      <template #table>
        <pure-table
          ref="tableRef"
          border
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :max-height="tableHeight"
          :checkList="['勾选列, 序号列']"
          :data="dataList"
          :columns="columns"
          :pagination="pagination"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)',
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button link type="primary" @click="handlePreview(row)"> 预览 </el-button>
            <el-button link type="primary" @click="handleEditTable(row)"> 编辑 </el-button>
            <el-button link type="primary" @click="handleGenTable(row)"> 生成代码 </el-button>
            <el-dropdown>
              <el-button class="ml-3 mt-[2px]" link type="primary" :icon="useRenderIcon(More)" />
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>
                    <el-button link type="primary" @click="handleDelete(row)"> 删除 </el-button>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-button link type="primary" @click="handleSynchDb(row)"> 同步 </el-button>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <!-- 预览界面 -->
    <el-dialog
      v-model="showPreviewDialog"
      :title="preview.title"
      width="80%"
      top="5vh"
      append-to-body
      class="scrollbar"
    >
      <el-tabs v-model="preview.activeName">
        <el-tab-pane
          v-for="key in Object.keys(preview.data)"
          :key="preview.data[key]"
          :label="key.substring(key.lastIndexOf('/') + 1, key.indexOf('.vm'))"
          :name="key.substring(key.lastIndexOf('/') + 1, key.indexOf('.vm'))"
        >
          <el-link
            v-copyText="preview.data[key]"
            v-copyText:callback="copyTextSuccess"
            :underline="false"
            icon="DocumentCopy"
            style="float: right"
            >&nbsp;复制</el-link
          >
          <pre>{{ preview.data[key] }}</pre>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <import-table-dialog v-model:visible="showImportTableDialog" @ok="onSearch" />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
